package com.taiyuan.tongcheng.ui;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.lbh.share.OnLoginToPlatformListener;
import net.lbh.share.OnShareListener;
import net.lbh.share.PlatformAccountAgent;
import net.lbh.share.ShareAgent;
import net.lbh.share.bean.ShareInfo;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.KJLoger;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.taiyuan.tongcheng.bean.MoneyCardDto;
import com.taiyuan.tongcheng.bean.PageInfo;
import com.taiyuan.tongcheng.http.HttpParamUtil;
import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.http.WebData;
import com.taiyuan.tongcheng.jsonparser.TcJsParser;
import com.tytc56.user.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.exception.SocializeException;

public class ShareActvity extends BaseActivity {

	@BindView(id = R.id.btn_share_wx, click = true)
	private Button wxBtn;
	@BindView(id = R.id.btn_share_wx_friend, click = true)
	private Button wxFriendBtn;
	@BindView(id = R.id.btn_share_qq, click = true)
	private Button qqBtn;
	@BindView(id = R.id.btn_share_sina, click = true)
	private Button sinaBtn;
	ShareInfo shareInfo;
	@BindView(id = R.id.share, click = true)
	private Button shareBtn;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_share);
	}

	@Override
	public void initData() {
		super.initData();
		shareInfo = new ShareInfo();
		shareInfo.setTitle("用同成配送叫车送货搬家真方便,大家快来试试吧");
		shareInfo.setLocalImgId(R.drawable.app_icon, ShareActvity.this);
		shareInfo.setText("我刚才配送货物，用了同成配送，方便靠谱，注册就送优惠券！");
		shareInfo.setUrl("http://a.app.qq.com/o/simple.jsp?pkgname=com.tytc56.user");
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initView();
		topTv.setText(R.string.user_share);
		rightTv.setVisibility(View.GONE);
		ShareAgent shareAgent = ShareAgent.getInstance();
		shareAgent.setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
				SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA);
		shareAgent.initWxKeys("wx79e95c3cb4556117",
				"9fb6b72c78484ac5e4f7785d783f59a2");
		shareAgent.initQQKeys("1104797315", "9p8yAzNDWJw1KGiu");
		shareAgent.setDebug(true);
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.btn_share_wx:
			ShareAgent.getInstance().share(ShareActvity.this, shareInfo,
					SHARE_MEDIA.WEIXIN, new OnShareListener() {

						@Override
						public void onSuccess() {

						}

						@Override
						public void onFail() {

						}
					});
			break;
		case R.id.btn_share_wx_friend:
			ShareAgent.getInstance().share(ShareActvity.this, shareInfo,
					SHARE_MEDIA.WEIXIN_CIRCLE, new OnShareListener() {

						@Override
						public void onSuccess() {
							
							getShareGift();
							
						}

						@Override
						public void onFail() {

						}
					});
			break;
		case R.id.btn_share_qq:
			ShareAgent.getInstance().share(ShareActvity.this, shareInfo,
					SHARE_MEDIA.QQ, new OnShareListener() {

						@Override
						public void onSuccess() {

						}

						@Override
						public void onFail() {

						}
					});
			break;
		case R.id.btn_share_sina:
			ShareAgent.getInstance().share(ShareActvity.this, shareInfo,
					SHARE_MEDIA.SINA, new OnShareListener() {

						@Override
						public void onSuccess() {

						}

						@Override
						public void onFail() {

						}
					});
			break;
		case R.id.share:
			ShareAgent.getInstance().share(ShareActvity.this, shareInfo,
					new OnShareListener() {

						@Override
						public void onSuccess() {

						}

						@Override
						public void onFail() {

						}
					});
		
			break;

		default:
			break;
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		ShareAgent.getInstance()
				.onActivityResult(requestCode, resultCode, data);
		PlatformAccountAgent.getInstance().onActivityResult(requestCode,
				resultCode, data);
	}
	/**
	 * 获取分享
	 */
	private void getShareGift() {

		HttpConfig config =new HttpConfig();
		config.cacheTime=0;
		KJHttp kjHttp =new KJHttp(config);
		HttpParams params= HttpParamUtil.getShareGift(getApplicationContext());
		KJLoger.debug(params.getUrlParams().toString());
		kjHttp.post(TcWebservice.WEBSERVER_IP+TcWebservice.API.ACCOUNT_GET_SHARE_GIFT, params, new HttpCallBack() {
			@Override
			public void onFailure(int errorNo, String strMsg) {
				super.onFailure(errorNo, strMsg);
			}
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					String response = URLDecoder.decode(t,"UTF-8");
					KJLoger.debug(response);
					WebData  data =new WebData();
					data=TcJsParser.getWedataData(response);
					if(data.getStatus()==1){
//						showCustomToast(data.toString());
						KJLoger.debug(data.toString());
					}else if(data.getStatus()==0){
					}else if(data.getStatus()==-1){
						showErroCustomToast(data.getMsg());
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			
		
		});
	
	}
	

}
